<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="12">
          <a-col :md="6" :sm="8">
            <a-form-item label="标题" :maxLength="50">
              <a-input
                placeholder="请输入标题"
                v-model="queryParam.title"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="8">
            <span
              style="float: left; overflow: hidden"
              class="table-page-search-submitButtons"
            >
              <a-button
                type="primary"
                @click="$refs.table.refresh(true)"
                icon="search"
                >查询</a-button
              >
              <a-button
                @click="
                  () => {
                    this.$objToReset(queryParam), $refs.table.refresh(true)
                  }
                "
                icon="reload"
                style="margin-left: 8px"
                >重置</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
      <s-table
        ref="table"
        rowKey="sncId"
        size="middle"
        :columns="columns"
        :data="loadData"
      >
        <template slot="action" slot-scope="text, record">
          <a @click="handelView(record)" v-wx-role="51505">
            <a-icon type="eye" />
            查看</a
          >
          <a-divider type="vertical" />
          <a-popconfirm
            title="确定删除吗?"
            @confirm="() => handleDelete(record.sncId)"
            v-wx-role="51504"
          >
            <a>删除</a>
          </a-popconfirm>
        </template>
      </s-table>
      <div class="table-operator">
        <a-button type="primary" @click="readAll" icon="book"
          >全部标准已读</a-button
        >
      </div>
      <my-notice-view ref="modalView" @ok="formModalOk"></my-notice-view>
    </div>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { deleteAction, getAction, postAction } from '@/api/manage'
import { baseMixin } from '@/store/app-mixin'
import MyNoticeView from './MyNoticeView'

export default {
  name: 'MyNoticeList',
  mixins: [baseMixin],
  components: { STable, Ellipsis, MyNoticeView },
  data() {
    return {
      visible: false,
      // 查询条件
      queryParam: {
        title: '',
      },
      // 表头
      columns: [
        {
          title: '标题',
          dataIndex: 'title',
        },
        {
          title: '消息类型',
          dataIndex: 'msgCategory',
          customRender: (text) => this.$sysDictFilter(text, 'msgCategory'),
        },
        {
          title: '发布人',
          dataIndex: 'createBy',
        },
        {
          title: '优先级',
          dataIndex: 'priority',
          customRender: (text) => this.$sysDictFilter(text, 'priority'),
        },
        {
          title: '发布时间',
          dataIndex: 'createTime',
          customRender: (text) => this.$momentFormat(text, 'YYYY-MM-DD'),
        },
        {
          title: '阅读状态',
          dataIndex: 'readFlag',
          customRender: (text) => this.$sysDictFilter(text, 'readFlag'),
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        console.log('loadData.parameter', parameter)
        return postAction(
          this.url.list,
          Object.assign(parameter, this.queryParam)
        ).then((res) => {
          return res.result
        })
      },
      dict: '',
      url: {
        list: '/system/sysNotice/myNoticeList',
        delete: '/system/sysNotice/myNoticeDelete',
        readAll: '/system/sysNotice/myNoticeViewAll',
      },
    }
  },
  computed: {},
  methods: {
    // 用户编辑
    handleEdit(record) {
      this.$refs.modalEdit.edit(record)
    },
    searchQuery() {
      this.$refs.table.refresh(true)
    },
    // 全部标记已读
    readAll() {
      getAction(this.url.readAll, {}).then((res) => {
        this.$message.success(res.content)
        this.$refs.table.refresh(true)
      })
    },
    // 查看字典数据
    handelView(record) {
      this.$refs.modalView.show(record)
    },
    // 删除
    handleDelete(record) {
      deleteAction(this.url.delete, { ids: record }).then((resp) => {
        this.$message.success('删除成功!')
        this.$refs.table.refresh(true)
      })
    },
    // 保存用户 回调
    formModalOk() {
      this.$refs.table.refresh()
    },
  },
  watch: {},
}
</script>
<style scoped></style>
